|
|
|
Character
constants are rarely used, since string constants are more convenient. A
string constant is surrounded by double quotes eg "Brian and
Dennis". The string is actually stored as an array of characters. The
null character '\0' is automatically placed at the end of such a string. This
acts as a string terminator.
|
|
A character is
not the same type as a single character string. This is important.
|
|
The rules for
numeric constants (integer, real) are essentially the same, though C is a bit
more flexible.
|
|
1.In C/C++ non-printing characters may be
embedded in either character or string constants by using various escapes –
e.g.
|
|
\0 – null character
|
|
\n – new line
|
|
\t – tab
|
|
\b – backspace
|
|
\f – formfeed
|
|
\\ - \
|
|
\’ – ‘
|
|
\” – “
|
|
\ddd – Character
whose OCTAL code is ddd – e.g. ‘\101’ is the same as ‘A’
|
|
2. String
constants are terminated by a null character \0. Thus, the total space
allocated for a string is one more than the number of characters in it.
|
|
3. String
constants may be continued over more than one line by having a \ be the very
last character on the line to be continued – e.g.
|
|
|
|
“This is a
string constant that ex\
|
|
Tends over more
than one line.”
|
|
|
|
C++ and some
ANSI C compilers only
|
|
4. A string
constant may be continued over more than one line by simply closing the
quotes on one line and re-opening them on the next – e.g.
|
|
|
|
“This is a
string constant that ex”
|
|
“tends over more
than one line.”
|